
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
	key_t key;
	struct msgbuf {
		long tp;
		char mm[100];
	} mybuf;
	int len, n, msgid;
	char buf[100];
	if((key = ftok("som.dat", 0)) < 0) {
		cout << "can't create key" << endl;
		exit(-1);
	}
	if((msgid = msgget(key, IPC_CREAT|0644)) < 0) {
		cout << "something is incorrect" << endl;
		exit(-1);
	}
	mybuf.tp = 1;
	while(1) {
		fgets(buf, 100, stdin);
		len = strlen(buf);
		strcpy(mybuf.mm, buf);
		if(msgsnd(msgid, &mybuf, len, 0) < 0) {
			msgctl(msgid, IPC_RMID, NULL);
			exit(-1);
		}
		if(!strcmp(buf, "exit\n")) break;
	} // for
	mybuf.tp = 300;
	len = 0;
	if((msgid = msgsnd(msgid, &mybuf, len, 0)) < 0) {
		msgctl(msgid, IPC_RMID, NULL);
		exit(-1);
	}
	return 0;
}
